package com.zwf.unifiedresponse.common;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.util.CollectionUtils;

import java.util.List;


public class ResponseListResult<T> extends ResponseResult<T> {

    @ApiModelProperty(
            value = "总记录数",
            example = "0",
            position = 10,
            required = true
    )
    private Long count = 0L;

    protected ResponseListResult() {
        super(ResponseCodeEnum.SUCCESS.getCode());
    }

    protected ResponseListResult(Long total, T rows) {
        super(ResponseCodeEnum.SUCCESS.getCode());
        this.count = total == null ? 0L : total;
        this.setData(rows);
    }

    protected ResponseListResult(T list) {
        super(ResponseCodeEnum.SUCCESS.getCode());
        if (list instanceof Page) {
            Page<?> page = (Page) list;
            this.count = page.getTotal();
            this.setData((T) page.getResult());
        } else {
            if (list instanceof List) {
                List<T> dataList = (List<T>) list;
                this.count = CollectionUtils.isEmpty(dataList) ? 0L : (long) dataList.size();
            }
            this.setData(list);
        }
    }

    public static <T> ResponseListResult<T> ok(Long total, T rows) {
        return new ResponseListResult<T>(total, rows);
    }

    public static <T> ResponseListResult<T> ok(T rows) {
        return new ResponseListResult<T>(rows);
    }

    public Long getCount() {
        return this.count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

}
